<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/message.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/task.jsp"%>
<%
    // 创建日程
    String team = UUID.randomUUID().toString();
    List<Arrangement> ags = new ArrayList<Arrangement>();
    String imgStr = input.getDesc("imgs", "");
    List imgs = new ArrayList();
    if (!isEmpty(imgStr)) {
        String[] strs = imgStr.split("\\|");
        for (int  i = 0; i < strs.length; i++) {
            imgs.add(strs[i]);
        }
    }
    Long notify = input.getLong("notify", -1L);
    long pushTime = 0;
    if (notify > 0) {
        String start = input.getDesc("start", "");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date d = sdf.parse(start);
		pushTime = d.getTime() - notify;
    }
    String patientsStr = input.getDesc("patients", "");
    List<Integer> uids = new ArrayList<Integer>();
    uids.add(token.getUid());
    if (!isEmpty(patientsStr)) {
        String[] strs = patientsStr.split("\\|");
        for (int  i = 0; i < strs.length; i++) {
            Arrangement ag = Arrangement.fromMap(parameters, Arrangement.class);
            ag.setUid(token.getUid());
            ag.setIsDone(0);
            ag.setImgs(JSONUtil.toJson(imgs));
            ag.setPartner(Tools.getInt(strs[i]));
            uids.add(ag.getPartner());
            ag.setPushTime(pushTime);
            ag.setPatients("Y");
            ag.setDoctors("N");
            ag.setTeam(team);
            ags.add(ag);
        }
    }
    String doctorStr = input.getDesc("doctors", "");
    List doctors = new ArrayList();
    if (!isEmpty(doctorStr)) {
        String[] strs = doctorStr.split("\\|");
        for (int  i = 0; i < strs.length; i++) {
            Arrangement ag = Arrangement.fromMap(parameters, Arrangement.class);
            ag.setUid(token.getUid());
            ag.setIsDone(0);
            ag.setImgs(JSONUtil.toJson(imgs));
            ag.setPartner(Tools.getInt(strs[i]));
            uids.add(ag.getPartner());
            ag.setPushTime(pushTime);
            ag.setPatients("N");
            ag.setDoctors("Y");
            ag.setTeam(team);
            ags.add(ag);
        }
    }
    List<Integer> ids = (List<Integer>)DBTools.db("basedb").addItem(ags);
    if (isEmpty(ids)) {
        msg = new Message(30071, "创建日程失败");
    }
    ags = QBuilder.db("basedb").select().from("Arrangement").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("team=", team)).query().getResultObj();
    MyTask task = new MyTask(ags.size());
    final Map<Integer, User> users = usersByUids(uids);
    for (int i = 0; i < ags.size(); i++) {
        Arrangement ag = ags.get(i);
        task.runTask(task.new MyRunnable(task.new DBOperate() {

				@Override
				public Object operate(Object... objects) {
                    Arrangement ag = (Arrangement)objects[0];
                    Information info = new Information();
                    info.setOid(ag.getId()+"");
                    info.setHospital(ag.getHospital());
                    String title = ag.getUid().equals(ag.getPartner()) ? "您创建了一份日程" : "您收到了一份日程";
                    info.setTitle(title);
                    info.setSumary("日程");
                    User user =  users.get(ag.getUid());
                    String author = user.getName();
                    user =  users.get(ag.getPartner());
                    String joiner = user.getName();
                    if (ag.getUid().equals(ag.getPartner())) {
                        info.setContent(author + "您好, 您创建了一份日程。\n内容:"+ag.getTitle()+"\n日期:"+ag.getStart());
                    } else {
                        info.setContent(joiner + "您好, 您参与了由"+author+"创建的日程。\n内容:"+ag.getTitle()+"\n日期:"+ag.getStart());
                    }
                    info.setCategory(4);
                    createMsg(info, ag.getPartner());
					return null;
				}
			}, i, new Object[]{ag}));
    }
    task.stopTask();
    msg.put("data", ids);
    Tools.encryptPrint(out, msg);
%>
